const app = getApp()
import { configApp } from '../../../utils/config'
const FN = require('../../../utils/public');
import commonAPI from '../../../api/api';
Page({
  data: {
    voiceObj: {},
    userInfo: {},
    qrcodeurl: '',
    myPageParams: {
      "learningDays": 0,
      "exerciseDays": 0,
      "arenaNum": 0,
    },
  },
  onLoad(options) {
    var that = this;
    this.myStatisticsData();
    this.doLoadFontFace();// 下载字体    
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', {});
    eventChannel.emit('someEvent', {});
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log(app);
      that.setData({
        userInfo: app.globalData.userInfo.signInfo,
        voiceObj: data.voiceObj
      });
      that.createPosterQRcode();// 海报二维码图片
    })
  },

  // 学习天数及口语练习次数
  myStatisticsData: function () {
    var that = this;
    commonAPI.statisticsData({}).then(res => {
      var learningDays = 'myPageParams.learningDays';
      var exerciseDays = 'myPageParams.exerciseDays';
      var arenaNum = 'myPageParams.arenaNum';
      that.setData({
        [learningDays]: res.data[0].studyCount,// 学习天数
        [exerciseDays]: res.data[0].practiceCount,// 口语练习
        [arenaNum]: res.data[0].arenaNum//参与挑战数据
      })
    });
  },


  createPosterQRcode: function () {
    var that = this;
    commonAPI.vrCreateQrcode4Poster({
      pageUrl: 'pages/homeworkrepeat/homeworkrepeat',
      voiceSystemId: that.data.voiceObj.voiceSystemId
    }).then(res => {
      if (res.code == '200') {
        that.setData({
          qrcodeurl: configApp.img_base_url + res.data
        })
      }
    })
  },

  doLoadFontFace() {
    wx.loadFontFace({
      family: "Monaco",
      source: 'url("https://minapp.leshijiewx.com/common/Monaco.woff")',// https://fonts.cdnfonts.com/s/14106/Monaco.woff
      scopes: ['webview', 'native'],
      success: (res) => {
          console.log('success', res.status)
          this.render()
      },
      fail: function (res) {
          console.error(res)
      },
      complete(res) {
        console.log('111', res)
      }
    });
  },

    // 返回
    gohome: function () {
      wx.navigateBack({
        delta: 1
      })
    },

    tap() {
      wx.loadFontFace({
        family: "Monaco", 
        source: 'url("https://minapp.leshijiewx.com/common/Monaco.woff")',// https://fonts.cdnfonts.com/s/14106/Monaco.woff
        scopes: ['webview', 'native'],
        success: (res) => {
            console.log('success', res.status)
            this.render()
        },
        fail: function (res) {
            console.error(res)
        },
        complete(res) {
          console.log('111', res)
        }
      });
  
      this.createSelectorQuery().select("#view")
        .node().exec(res => {
          const node = res[0].node
          node.takeSnapshot({
            // type: 'file' 且 format: 'png' 时，可直接导出成临时文件
            type: 'arraybuffer',
            format: 'png',
            success: (res) => {
              const f = `${wx.env.USER_DATA_PATH}/hello.png`
              const fs = wx.getFileSystemManager();
              fs.writeFileSync(f, res.data, 'binary')
              wx.showToast({
                title: '保存成功'
              })
  
              wx.saveImageToPhotosAlbum({
                filePath: f,
                complete(res) {
                  console.log("saveImageToPhotosAlbum:", res)
                }
              })
            },
            fail(res) {
              console.log("takeSnapshot fail:", res)
            }
          })
        })
    }
})
